<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="店铺状态, VALID: 有效, INVALID: 禁用, DELETED: 删除" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择店铺状态, VALID: 有效, INVALID: 禁用, DELETED: 删除" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['d3:shop:add']"-->
<!--        >新增</el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['d3:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['d3:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['d3:shop:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺名" align="center" prop="name" />
      <el-table-column label="销售订单客户" align="center" prop="customerName" />
      <el-table-column label="销售订单发起部门" align="center" prop="deptName" width="160"/>
      <el-table-column label="销售订单发起人" align="center" prop="nickName" width="120"/>
      <el-table-column label="开始同步时间" align="center" prop="syncDate"/>
      <el-table-column label="同步规则" align="center" prop="syncRate"/>
      <el-table-column label="同步信息" align="center" prop="remark"/>
<!--      <el-table-column label="状态" align="center" prop="logisticRuleName"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['d3:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['d3:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改D3平台店铺对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="店铺名" prop="name">
          <el-input v-model="form.name" placeholder="请输入店铺名" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="店铺平台类型" prop="platform">
          <el-input v-model="form.platform" placeholder="请输入店铺平台类型" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="销售订单客户" prop="customerName">
          <el-select v-model="form.customerId" placeholder="请选择销售订单客户" style="width: 380px;" clearable filterable>
            <el-option
            v-for="item in customers"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="销售订单发起部门" prop="deptName">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择销售订单发起部门" style="width: 380px;"/>
        </el-form-item>
        <el-form-item label="销售订单发起人" prop="nickName">
          <el-select v-model="form.submitterId" placeholder="请选择销售订单发起人" style="width: 380px;" clearable filterable>
            <el-option
              v-for="item in users"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="同步规则" prop="syncRate">
          <el-input v-model="form.syncRate" placeholder="请输入同步规则" style="width: 380px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop, exportShop } from "@/api/d3/shop";
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getCustomerList} from "@/api/public";
import {getUserList} from "@/api/public";

export default {
  name: "Shop",
  components: {Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      id:null,
      disabled:false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // D3平台店铺表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      //部门下拉树结构
      deptOptions:[],
      //客户下拉列表
      customers:[],
      //用户下拉列表
      users:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        title: null,
        platform: null,
        platformtype: null,
        type: null,
        createtime: null,
        status: null,
        refuserid: null,
        refusernick: null,
        refauthstatus: null,
        refauthexpirestime: null,
        customerId: null,
        customerName: null,
      },
      // 表单参数
      form: {
        deptId:'',
      },
      // 表单校验
      rules: {
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询D3平台店铺列表 */
    getList() {
      // this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        console.log(this.shopList)
        this.total = response.total;
        this.loading = false;
      });
    },

    getTreeselect(){
      treeselect().then(response =>{
        this.deptOptions=response.data;
      });
    },

    getCustomers(){
      getCustomerList({status:1}).then(response=>{
        this.customers=response.data;
        // console.log(this.customers)
      })
    },

    getUsers() {
      getUserList().then(response=>{
        this.users=response.data;
        // console.log(this.users)
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        title: null,
        platform: null,
        platformtype: null,
        type: null,
        createtime: null,
        status: "0",
        refuserid: null,
        refusernick: null,
        refauthstatus: "0",
        refauthexpirestime: null,
        customerId: null,
        customerName: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加D3平台店铺";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      this.getCustomers();
      this.getUsers();
      this.disabled=true;
      const id = row.id || this.ids
      getShop(id).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.open = true;
        this.title = "修改D3平台店铺";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShop(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sIds = row.id || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delShop(sIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有D3平台店铺数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportShop(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
